//
//  RWCollectionViewController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/26.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWCollectionViewController.h"
#import "RWCollectionTableViewCell.h"
#import "SegmentSuperViewController.h"
#import "FTYNavigationController.h"
#import "RWLoginViewController.h"

#import "RWGoodsModel.h"

@interface RWCollectionViewController ()<RWCollectionTableViewCellDelegate>

@property (nonatomic, copy) NSMutableArray *collectionModels;

@end

@implementation RWCollectionViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self configNotification];
}

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    if ([FTYUser sharedUser].userID.length > 0) {
        
        [self loadDatas];
    } else {
    
        RWLoginViewController *loginVC = [[RWLoginViewController alloc] init];
        FTYNavigationController *loginNC = [[FTYNavigationController alloc] initWithRootViewController:loginVC];
        loginNC.navigationBar.translucent = NO;
        loginNC.navigationBar.barTintColor = [FTYColor getMainColorWithAlpha:1.];
        loginNC.navigationBar.tintColor = [UIColor whiteColor];
        [loginNC.navigationBar setTitleTextAttributes:@{NSFontAttributeName: FTY_FONTTSIZE(20),
                                                        NSForegroundColorAttributeName: [UIColor whiteColor]}];
        [self presentViewController:loginNC animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{

    _collectionModels = [NSMutableArray array];
}

- (void)loadDatas{
    
    [_collectionModels removeAllObjects];
    if (_collectionTV) {
        [_collectionTV removeFromSuperview];
    }

    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=goodslist&type=coll&member_id=%@", [FTYUser sharedUser].userID] parameters:nil block:^(id result, NSError *error) {
        if (!error) {
            
            DLog(@"====%@", result);
            FTYBaseModel *baseModel = [[FTYBaseModel alloc] init];
            baseModel = [FTYBaseModel mj_objectWithKeyValues:result];
            FTYErrorInfoModel *errorInfo = [FTYErrorInfoModel mj_objectWithKeyValues:baseModel.err];
            
            if (0 == errorInfo.errorcode) {
                
                [_collectionModels addObjectsFromArray:[FTYBaseModel mj_objectWithKeyValues:result].info];
                
                [self setNavigation];
                [self setCollectionUI];
            } else {
                [[FTYToastView alloc] addTitle:errorInfo.errorinfo addView:self.view];
            }
        }  else {
            
            NSLog(@"收藏列表请求错误：%@", error);
        }
    }];
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"关注";
}

- (void)setCollectionUI{
    
    _collectionTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT - FTY_TABBAR_HEIGHT - 85*FTY_HEIGHT_BASE) style:UITableViewStylePlain];
    [self.view addSubview:_collectionTV];
    _collectionTV.dataSource = self;
    _collectionTV.delegate = self;
    _collectionTV.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - Config Invoke Methods

#pragma mark - Notificaiton Invoke Method

- (void)configNotification{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeCollection) name:@"CollectionNotification" object:nil];
}

- (void)homeCollection{

    [self loadDatas];
}

#pragma mark - Private Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _collectionModels.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 150.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * const collectionID = @"collectionID";
    RWCollectionTableViewCell *collectionCell = [tableView dequeueReusableCellWithIdentifier:collectionID];
    if (!collectionCell) {
        collectionCell = [[RWCollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:collectionID];
        collectionCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    collectionCell.delegate = self;
    collectionCell.tag = indexPath.row;
    [collectionCell loadDataWithModel:_collectionModels[indexPath.row]];
    return collectionCell;
}

#pragma mark - <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    RWGoodsModel *goodsModel = [RWGoodsModel mj_objectWithKeyValues:_collectionModels[indexPath.row]];
    SegmentSuperViewController *detailPageVC = [[SegmentSuperViewController alloc] init];
    detailPageVC.hidesBottomBarWhenPushed = YES;
    detailPageVC.goodsID = goodsModel.goods_id;
    [self.navigationController pushViewController:detailPageVC animated:YES];
}

#pragma mark - <RWCollectionTableViewCellDelegate>

- (void)collection:(UIButton *)btn atIndex:(NSInteger)index{

    RWGoodsModel *goodsModel = [RWGoodsModel mj_objectWithKeyValues:_collectionModels[index]];
    [FTYCollectionPort requestCancelCollectionWithProjectID:goodsModel.goods_id viewController:self block:^(BOOL isBool) {
        
        [_collectionModels removeObjectAtIndex:index];
        [_collectionTV reloadData];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CollectionCancelToHomepageNotification" object:nil];
    }];
}

#pragma mark - Private Invoke Methods

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
